package com.pingtai.manage.service;


import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.pingtai.ajax.entity.Notice;
import com.pingtai.dao.AbstractManager;
import com.pingtai.indent.service.WeiboIndentService;
import com.pingtai.manage.dao.NoticeDao;
import com.pingtai.user.entity.User;
import com.pingtai.user.service.UserManagerService;
import com.pingtai.util.PaginationSupport;
import com.pingtai.xinlang.login.entity.LoginUser;

public class NoticeService {
	
	private UserManagerService userManagerService;
	private WeiboIndentService weiboIndentService;
	private NoticeDao noticeManageDao;
	private AbstractManager abstractManager;
	
	
	
	public AbstractManager getAbstractManager() {
		return abstractManager;
	}
	public void setAbstractManager(AbstractManager abstractManager) {
		this.abstractManager = abstractManager;
	}
	public UserManagerService getUserManagerService() {
		return userManagerService;
	}
	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}
	public WeiboIndentService getWeiboIndentService() {
		return weiboIndentService;
	}
	public void setWeiboIndentService(WeiboIndentService weiboIndentService) {
		this.weiboIndentService = weiboIndentService;
	}
	public NoticeDao getNoticeManageDao() {
		return noticeManageDao;
	}
	public void setNoticeManageDao(NoticeDao noticeManageDao) {
		this.noticeManageDao = noticeManageDao;
	}

	public PaginationSupport getNoticeForPage(Integer pageName){
		Session session = noticeManageDao.getSession();
		Criteria criteria = session.createCriteria(Notice.class);

		User user = userManagerService.findCurrectUser();
		
		criteria.addOrder(
				Order.desc("id"));
		return abstractManager.findPageByCriteria(criteria, pageName);
	}
	
	public PaginationSupport getLoginUserForPage(Integer pageName){
		Session session = noticeManageDao.getSession();
		Criteria criteria = session.createCriteria(LoginUser.class);

		User user = userManagerService.findCurrectUser();
		
		criteria.addOrder(
				Order.desc("id"));
		return abstractManager.findPageByCriteria(criteria, pageName);
	}
	
	
	public void addNotice(Notice notice){
		User user = userManagerService.findCurrectUser();
		notice.setCreateUser(user);
		notice.setCreateDate(new Date());
		noticeManageDao.save(notice);
	}
	
	public void updateNotice(Notice notice){
		User user = userManagerService.findCurrectUser();
		notice.setChangeUser(user);
		notice.setChangeDate(new Date());
		noticeManageDao.update(notice);
	}
	
	public void updateLoginUser(LoginUser loginUser){
		noticeManageDao.update(loginUser);
	}
	
	public Notice getNoticeById(Integer id){
		return (Notice) noticeManageDao.getObject(Notice.class, id);
	}
	public LoginUser getCookieById(Integer id){
		return (LoginUser) noticeManageDao.getObject(LoginUser.class, id);
	}
	
	
}
